package com.wz.base.support.events.publisher;

import com.wz.base.support.events.DomainEvent;
import com.wz.base.support.events.DomainEventPublisher;
import io.micrometer.core.instrument.MeterRegistry;

public class MeteredDomainEventPublisher implements DomainEventPublisher {

    private static final String DOMAIN_EVENTS = "domain_events";
    private static final String TAG_NAME = "name";

    private final DomainEventPublisher delegate;
    private final MeterRegistry metricsRegistry;

    public MeteredDomainEventPublisher(DomainEventPublisher delegate, MeterRegistry metricsRegistry) {
        this.delegate = delegate;
        this.metricsRegistry = metricsRegistry;
    }

    @Override
    public void publish(DomainEvent event) {
        delegate.publish(event);
        metricsRegistry.counter(DOMAIN_EVENTS, TAG_NAME, event.getClass().getSimpleName()).increment();
    }
}
